package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;


@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    @RequestMapping("/submit")
    public Result submit(@RequestBody Map map){
        /*
        * 前端提交请求信息给后端，因为后端没有对应的pojo实体类
        * 所以前端发给后端的请求 用map集合进行封装
        * 1.拿到前端请求信息后，第一件事，从redis中获取之前保存的验证码
        * 2.与当前前台用户输入传来的验证码进行比对
        * 3.如果比对成功，在继续进行之后service 和dao层的业务处理
        * 4.如果比对失败 ，返回给前台页面错误信息
        * */
        String telephone = (String) map.get("telephone");
        String validateCode = (String) map.get("validateCode");
        String CodeinRedis = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        if(validateCode==null || CodeinRedis==null || !CodeinRedis.equals(validateCode)){
            return new Result(false,MessageConstant.VALIDATECODE_ERROR);
        }
//        验证通过继续执行后面写入数据库的逻辑操作
            Result result = null;

        try{
            map.put("orderType", Order.ORDERTYPE_WEIXIN);
            result =  orderService.order(map);
        }catch (Exception e){
            e.printStackTrace();
            return  new Result(false,"服务器正忙，请稍后访问");
        }

//        如果预约成功就发短信提示用户预约成功
        if(result.isFlag()){
            System.out.println();
            String date = (String) map.get("orderDate");
            try{
//                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone,date);
                System.out.println("恭喜您预约成功你的预约时间是"+date);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return  result;
    }


    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Map map = orderService.findById(id);
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,map);

        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,"服务器正忙，请稍后访问");
        }
    }

@RequestMapping("/check")
    public Result login(@RequestBody Map map){
        String telephone = (String) map.get("telephone");
        String validateCode = (String) map.get("validateCode");
        String CodeinRedis = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
        if(validateCode ==null || CodeinRedis==null || !CodeinRedis.equals(validateCode)){
            return new Result(false,MessageConstant.VALIDATECODE_ERROR);
        }
        return  new Result(true,MessageConstant.LOGIN_SUCCESS);
    }
}
